#include "callback.h"

void
on_window1_destroy(GtkWidget *widget, gpointer user_data)
{
    mysql_close(conn);
    gtk_main_quit();
}

void
on_window1_show(GtkWidget *widget, gpointer user_data)
{
    conn=mysql_init(NULL);
    mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
}

void
on_exit_clicked(GtkToolButton *widget, gpointer user_data)
{
    mysql_close(conn);
    gtk_main_quit();
}

void
on_add_clicked(GtkWidget *widget, gpointer user_data)
{
    tmbh=1;

    gtk_dialog_run(GTK_DIALOG(dialog2));
    gtk_widget_hide(GTK_WIDGET(dialog2));

}

void
on_edit_clicked(GtkWidget *widget, gpointer user_data)
{
    tmbh=2;

    GtkListStore *store;
    GtkTreeModel *model;
    GtkTextBuffer *buffer;
    GtkTreeIter  iter;
    gchar *ID_KOL, *JUDUL,*kolJENIS,*kolKATEGORI,*kolPENERBIT,*TAHUN,*DESKRIPSI;

    gtk_window_set_title(GTK_WINDOW(dialog2),"Edit Koleksi");

    //coba2
    selection=gtk_tree_view_get_selection(GTK_TREE_VIEW(kolView));
    //jenisView=setup_jenis_view();
    store = GTK_LIST_STORE(gtk_tree_view_get_model(
                               GTK_TREE_VIEW (kolView))); //create_list();

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (kolView));

    if (gtk_tree_model_get_iter_first(model, &iter) == FALSE)
        return;

    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection),
                                        &model, &iter))
    {
        //gtk_list_store_remove(store, &iter);
        gtk_tree_model_get(model, &iter,0,&ID_KOL,1,&JUDUL,2,&kolJENIS,3,&kolKATEGORI,4,&kolPENERBIT,5,&TAHUN,6,&DESKRIPSI,-1);


        gtk_widget_set_sensitive(entryID,FALSE);

        gtk_entry_set_text(GTK_ENTRY(entryID),ID_KOL);
        gtk_entry_set_text(GTK_ENTRY(entryJdl),JUDUL);
        gtk_entry_set_text(GTK_ENTRY(tahun),TAHUN);
        gtk_combo_box_set_active(GTK_COMBO_BOX(cbJenis),-1);
        gtk_combo_box_set_active(GTK_COMBO_BOX(cbKategori),-1);
        gtk_combo_box_set_active(GTK_COMBO_BOX(cbPenerbit),-1);
        //gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fileChooser),"");

        //buffer
        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(desText),GTK_WRAP_WORD);
        gtk_widget_set_sensitive(desText,FALSE);
        buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(desText));
        gtk_text_buffer_set_text(buffer,DESKRIPSI,-1);
        gtk_text_buffer_set_modified(buffer,FALSE);
        gtk_widget_set_sensitive(desText,TRUE);

        gtk_widget_set_sensitive(entryID,FALSE);
        gtk_widget_set_sensitive(entryJdl,TRUE);

        gtk_widget_set_sensitive(btnSimpanKat,TRUE);
        gtk_widget_set_sensitive(btnBatalKat,TRUE);
        gtk_widget_set_sensitive(btnTambahKat,FALSE);
        gtk_widget_set_sensitive(btnEditKat,FALSE);
        gtk_widget_set_sensitive(btnHapusKat,FALSE);
        gtk_widget_grab_focus(entryJdl);

        g_free(ID_KOL);
        g_free(JUDUL);
        g_free(kolJENIS);
        g_free(kolKATEGORI);
        g_free(kolPENERBIT);
        g_free(TAHUN);
        g_free(DESKRIPSI);
    }

    gtk_dialog_run(GTK_DIALOG(dialog2));
    gtk_widget_hide(GTK_WIDGET(dialog2));
}

void
on_about_clicked()
{
    gtk_dialog_run(GTK_DIALOG(aboutdialog));
    gtk_widget_hide(GTK_WIDGET(aboutdialog));
}

void
on_admin_clicked(GtkWidget* widget, gpointer user_data)
{
    gtk_dialog_run(GTK_DIALOG(jenis));
    gtk_widget_hide(GTK_WIDGET(jenis));
    on_btnBatalJns_clicked(widget, user_data);
}

void
on_server_clicked(GtkWidget* widget, gpointer user_data)
{
    gtk_dialog_run(GTK_DIALOG(penerbit));
    gtk_widget_hide(GTK_WIDGET(penerbit));
    //on_btnBatalJns_clicked(widget, user_data);
}

void
on_preference_clicked(GtkWidget* widget, gpointer user_data)
{
    gtk_dialog_run(GTK_DIALOG(kategori));
    gtk_widget_hide(GTK_WIDGET(kategori));
    //on_btnBatalJns_clicked(widget, user_data);
}

void
print_error (MYSQL *conn, char *message)
{
    fprintf (stderr, "%s\n", message);
    if (conn != NULL)
    {
        fprintf (stderr, "Error %u (%s)\n",
                 mysql_errno (conn), mysql_error (conn));
    }
}

//jenis
GtkWidget*
setup_jenis_view ()
{
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    GtkTreeModel *model;
    GtkWidget *jenistreeview;

    jenistreeview = gtk_tree_view_new ();
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("ID", renderer, "text", ID_JENIS, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (jenistreeview), column);

    //render column 2
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Jenis", renderer, "text", JENIS, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (jenistreeview), column);

    model=createJenislist();

    /* Add the tree model to the tree view and unreference it so that the model will
    * be destroyed along with the tree view. */
    gtk_tree_view_set_model (GTK_TREE_VIEW (jenistreeview), model);
    g_object_unref (model);
    return jenistreeview;
}

void
on_btnTambahJns_clicked(GtkWidget *widget, gpointer user_data)
{
    tmbh=1;
    gtk_entry_set_text(GTK_ENTRY(entryIDjns),"");
    gtk_entry_set_text(GTK_ENTRY(entryNamaJns),"");

    gtk_widget_set_sensitive(entryIDjns,FALSE);
    gtk_widget_set_sensitive(entryNamaJns,TRUE);
    gtk_widget_set_sensitive(btnSimpanJns,TRUE);
    gtk_widget_set_sensitive(btnBatalJns,TRUE);
    gtk_widget_set_sensitive(btnTambahJns,FALSE);
    gtk_widget_set_sensitive(btnEditJns,FALSE);
    gtk_widget_set_sensitive(btnHapusJns,FALSE);
    gtk_widget_grab_focus(entryNamaJns);
}

void
on_btnSimpanJns_clicked(GtkWidget *widget, gpointer user_data)
{
    const gchar *id = gtk_entry_get_text(GTK_ENTRY(entryIDjns));
    const gchar *jns = gtk_entry_get_text(GTK_ENTRY(entryNamaJns));

    char query[2000];
    if(tmbh==1)
    {
        //const char *jns = gtk_entry_get_text(entryNamaJns);
        sprintf(query,"insert into jenis(nama_jenis) values('%s')",jns);

        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
        if(mysql_query(conn, query)!=0)
        {
            /*ini mestinya pakai dialog*/
            print_error(conn, "INSERT statement failed");
        }
        else
        {
            /*ini juga harus pakai dialog*/
            printf("INSERT statement succeeded: %lu rows affected\n",
                   (unsigned long) mysql_affected_rows(conn));
        }
    }
    else if(tmbh==2)
    {

        sprintf(query,"update jenis set nama_jenis='%s' where id_jenis=%s ",jns,id);

        //g_print("%s",query);
        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
        if(mysql_query(conn, query)!=0)
        {
            /*ini mestinya pakai dialog*/
            print_error(conn, "INSERT statement failed");
        }
        else
        {
            /*ini juga harus pakai dialog*/
            printf("INSERT statement succeeded: %lu rows affected\n",
                   (unsigned long) mysql_affected_rows(conn));
        }
    }
    //mysql_close(conn);
}

void
on_btnBatalJns_clicked(GtkWidget *widget, gpointer user_data)
{
    //tmbh=1;
    gtk_entry_set_text(GTK_ENTRY(entryIDjns),"");
    gtk_entry_set_text(GTK_ENTRY(entryNamaJns),"");

    gtk_widget_set_sensitive(entryIDjns,FALSE);
    gtk_widget_set_sensitive(entryNamaJns,FALSE);
    gtk_widget_set_sensitive(btnSimpanJns,FALSE);
    gtk_widget_set_sensitive(btnBatalJns,FALSE);
    gtk_widget_set_sensitive(btnTambahJns,TRUE);
    gtk_widget_set_sensitive(btnEditJns,TRUE);
    gtk_widget_set_sensitive(btnHapusJns,TRUE);
}

void
on_btnEditJns_clicked(GtkWidget *widget, gpointer user_data)
{
    tmbh=2;
    GtkListStore *store;
    GtkTreeModel *model;
    GtkTreeIter  iter;
    gchar *ID,*JENIS; //menggunakan pointer

    //coba2
    selection=gtk_tree_view_get_selection(GTK_TREE_VIEW(jenisView));
    store = GTK_LIST_STORE(gtk_tree_view_get_model(
                               GTK_TREE_VIEW (jenisView))); //create_list();
    model = gtk_tree_view_get_model (GTK_TREE_VIEW (jenisView));

    if (gtk_tree_model_get_iter_first(model, &iter) == FALSE)
        return;

    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection),
                                        &model, &iter))
    {
        //gtk_list_store_remove(store, &iter);
        gtk_tree_model_get(model, &iter,0,&ID,1,&JENIS,-1);

        g_print("%s is selected \n %s\n",ID,JENIS);
        gtk_entry_set_text(GTK_ENTRY(entryIDjns),ID);
        gtk_entry_set_text(GTK_ENTRY(entryNamaJns),JENIS);

        gtk_widget_set_sensitive(entryIDjns,FALSE);
        gtk_widget_set_sensitive(entryNamaJns,TRUE);
        gtk_widget_set_sensitive(btnSimpanJns,TRUE);
        gtk_widget_set_sensitive(btnBatalJns,TRUE);
        gtk_widget_set_sensitive(btnTambahJns,FALSE);
        gtk_widget_set_sensitive(btnEditJns,FALSE);
        gtk_widget_set_sensitive(btnHapusJns,FALSE);
        gtk_widget_grab_focus(entryNamaJns);

        g_free(ID);
        g_free(JENIS);
    }


}

void
on_btnHapusJns_clicked(GtkWidget *widget, gpointer selection)
{
    GtkListStore *store;
    GtkTreeModel *model;

    GtkTreeIter  iter;
    gchar *ID; //menggunakan pointer
    //coba2
    selection=gtk_tree_view_get_selection(GTK_TREE_VIEW(jenisView));
    //jenisView=setup_jenis_view();
    store = GTK_LIST_STORE(gtk_tree_view_get_model(
                               GTK_TREE_VIEW (jenisView))); //create_list();

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (jenisView));

    if (gtk_tree_model_get_iter_first(model, &iter) == FALSE)
        return;

    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection),
                                        &model, &iter))
    {
        //gtk_list_store_remove(store, &iter);
        gchar query[2000];
        gtk_tree_model_get(model, &iter,ID_JENIS,&ID,-1);

        sprintf(query,"delete from jenis where id_jenis='%s'",ID);

        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
        if(mysql_query(conn, query)!=0)
        {
            /*ini mestinya pakai dialog*/
            print_error(conn, "INSERT statement failed");
        }
        else
        {
            /*ini juga harus pakai dialog*/
            printf("INSERT statement succeeded: %lu rows affected\n",
                   (unsigned long) mysql_affected_rows(conn));
        }
        g_free(ID);
        //mysql_close(conn);
    }
}

void
on_btnCloseJns_clicked(GtkWidget* widget, gpointer user_data)
{
    gtk_widget_hide(GTK_WIDGET(jenis));
}

//penerbit
void
on_btnTambahPen_clicked(GtkWidget *widget, gpointer user_data)
{
    GtkTextBuffer *buffer;
    tmbh=1;
    gtk_entry_set_text(GTK_ENTRY(entryIDPen),"");
    gtk_entry_set_text(GTK_ENTRY(entryNamaPen),"");
    //buffer
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(penTextview),GTK_WRAP_WORD);
    gtk_widget_set_sensitive(penTextview,FALSE);
    buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(penTextview));
    gtk_text_buffer_set_text(buffer,"",-1);
    gtk_text_buffer_set_modified(buffer,FALSE);
    gtk_widget_set_sensitive(penTextview,TRUE);

    gtk_widget_set_sensitive(entryIDPen,FALSE);
    gtk_widget_set_sensitive(entryNamaPen,TRUE);
    gtk_widget_set_sensitive(btnSimpanPen,TRUE);
    gtk_widget_set_sensitive(btnBatalPen,TRUE);
    gtk_widget_set_sensitive(btnTambahPen,FALSE);
    gtk_widget_set_sensitive(btnEditPen,FALSE);
    gtk_widget_set_sensitive(btnHapusPen,FALSE);
    gtk_widget_grab_focus(entryNamaPen);
}

void
on_btnSimpanPen_clicked(GtkWidget *widget, gpointer user_data)
{
    GtkTextBuffer *buffer;
    gchar *text;
    GtkTextIter  start, end;
    const gchar *id = gtk_entry_get_text(GTK_ENTRY(entryIDPen));
    const gchar *pen = gtk_entry_get_text(GTK_ENTRY(entryNamaPen));
    //buffer
    gtk_widget_set_sensitive (penTextview,FALSE);
    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (penTextview));
    gtk_text_buffer_get_start_iter (buffer, &start);
    gtk_text_buffer_get_end_iter (buffer, &end);
    text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
    gtk_text_buffer_set_modified (buffer, FALSE);
    gtk_widget_set_sensitive (penTextview, TRUE);


    char query[2000];
    if(tmbh==1)
    {
        sprintf(query,"insert into penerbit(nama_pen,lokasi) values('%s','%s')",pen,text);

        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
        if(mysql_query(conn, query)!=0)
        {
            /*ini mestinya pakai dialog*/
            print_error(conn, "INSERT statement failed");
        }
        else
        {
            /*ini juga harus pakai dialog*/
            printf("INSERT statement succeeded: %lu rows affected\n",
                   (unsigned long) mysql_affected_rows(conn));
        }
        g_free(text);
    }
    else if(tmbh==2)
    {
        sprintf(query,"update jenis set nama_jenis='%s' lokasi='%s' where id_jenis=%s ",pen,text,id);

        //g_print("%s",query);
        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
        if(mysql_query(conn, query)!=0)
        {
            /*ini mestinya pakai dialog*/
            print_error(conn, "INSERT statement failed");
        }
        else
        {
            /*ini juga harus pakai dialog*/
            printf("INSERT statement succeeded: %lu rows affected\n",
                   (unsigned long) mysql_affected_rows(conn));
        }
        g_free(text);
    }
    on_btnBatalPen_clicked(widget,user_data);
    //mysql_close(conn);
}

void
on_btnBatalPen_clicked(GtkWidget *widget, gpointer user_data)
{
    GtkTextBuffer *buffer;

    gtk_entry_set_text(GTK_ENTRY(entryIDPen),"");
    gtk_entry_set_text(GTK_ENTRY(entryNamaPen),"");
    //buffer
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(penTextview),GTK_WRAP_WORD);
    gtk_widget_set_sensitive(penTextview,FALSE);
    buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(penTextview));
    gtk_text_buffer_set_text(buffer,"",-1);
    gtk_text_buffer_set_modified(buffer,FALSE);
    gtk_widget_set_sensitive(penTextview,TRUE);

    gtk_widget_set_sensitive(entryIDPen,FALSE);
    gtk_widget_set_sensitive(entryNamaPen,FALSE);
    gtk_widget_set_sensitive(btnSimpanPen,FALSE);
    gtk_widget_set_sensitive(btnBatalPen,FALSE);
    gtk_widget_set_sensitive(btnTambahPen,TRUE);
    gtk_widget_set_sensitive(btnEditPen,TRUE);
    gtk_widget_set_sensitive(btnHapusPen,TRUE);
}

void
on_btnEditPen_clicked(GtkWidget *widget, gpointer user_data)
{
    tmbh=2;
    GtkTextBuffer *buffer;
    GtkListStore *store;
    GtkTreeModel *model;

    GtkTreeIter  iter;
    gchar *ID,*PENERBIT,*ALAMAT; //menggunakan pointer

    //coba2
    selection=gtk_tree_view_get_selection(GTK_TREE_VIEW(penView));
    store = GTK_LIST_STORE(gtk_tree_view_get_model(
                               GTK_TREE_VIEW (penView))); //create_list();

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (penView));

    if (gtk_tree_model_get_iter_first(model, &iter) == FALSE)
        return;

    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection),
                                        &model, &iter))
    {
        //gtk_list_store_remove(store, &iter);
        gtk_tree_model_get(model, &iter,0,&ID,1,&PENERBIT,2,&ALAMAT,-1);

        g_print("%s is selected \n %s\n %s\n",ID,PENERBIT,ALAMAT);
        gtk_entry_set_text(GTK_ENTRY(entryIDPen),ID);
        gtk_entry_set_text(GTK_ENTRY(entryNamaPen),PENERBIT);
        //buffer
        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(penTextview),GTK_WRAP_WORD);
        gtk_widget_set_sensitive(penTextview,FALSE);
        buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(penTextview));
        gtk_text_buffer_set_text(buffer,ALAMAT,-1);
        gtk_text_buffer_set_modified(buffer,FALSE);
        gtk_widget_set_sensitive(penTextview,TRUE);
        //masih saatu lagi textbuffer
        gtk_widget_set_sensitive(entryIDPen,FALSE);
        gtk_widget_set_sensitive(entryNamaPen,TRUE);
        gtk_widget_set_sensitive(btnSimpanPen,TRUE);
        gtk_widget_set_sensitive(btnBatalPen,TRUE);
        gtk_widget_set_sensitive(btnTambahPen,FALSE);
        gtk_widget_set_sensitive(btnEditPen,FALSE);
        gtk_widget_set_sensitive(btnHapusPen,FALSE);
        gtk_widget_grab_focus(entryNamaPen);

        g_free(ID);
        g_free(PENERBIT);
        g_free(ALAMAT);
    }
}

void
on_btnHapusPen_clicked(GtkWidget *widget, gpointer selection)
{
    GtkListStore *store;
    GtkTreeModel *model;

    GtkTreeIter  iter;
    gchar *ID; //menggunakan pointer
    //coba2
    selection=gtk_tree_view_get_selection(GTK_TREE_VIEW(penView));
    store = GTK_LIST_STORE(gtk_tree_view_get_model(
                               GTK_TREE_VIEW (penView))); //create_list();

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (penView));

    if (gtk_tree_model_get_iter_first(model, &iter) == FALSE)
        return;

    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection),
                                        &model, &iter))
    {
        gchar query[2000];
        gtk_tree_model_get(model, &iter,ID_PEN,&ID,-1);

        sprintf(query,"delete from penerbit where id_penerbit='%s'",ID);

        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
        if(mysql_query(conn, query)!=0)
        {
            /*ini mestinya pakai dialog*/
            print_error(conn, "INSERT statement failed");
        }
        else
        {
            /*ini juga harus pakai dialog*/
            printf("INSERT statement succeeded: %lu rows affected\n",
                   (unsigned long) mysql_affected_rows(conn));
        }
        g_free(ID);
    }
    //mysql_close(conn);
}

void
on_btnClosePen_clicked(GtkWidget* widget, gpointer user_data)
{
    gtk_widget_hide(GTK_WIDGET(penerbit));
}

//kategori
void
on_btnTambahKat_clicked(GtkWidget *widget, gpointer user_data)
{
    GtkTextBuffer *buffer;
    tmbh=1;
    gtk_entry_set_text(GTK_ENTRY(entryIDKat),"");
    gtk_entry_set_text(GTK_ENTRY(entryNamaKat),"");

    //buffer
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(katTextview),GTK_WRAP_WORD);
    gtk_widget_set_sensitive(katTextview,FALSE);
    buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(katTextview));
    gtk_text_buffer_set_text(buffer,"",-1);
    gtk_text_buffer_set_modified(buffer,FALSE);
    gtk_widget_set_sensitive(katTextview,TRUE);

    gtk_widget_set_sensitive(entryIDKat,FALSE);
    gtk_widget_set_sensitive(entryNamaKat,TRUE);
    gtk_widget_set_sensitive(btnSimpanKat,TRUE);
    gtk_widget_set_sensitive(btnBatalKat,TRUE);
    gtk_widget_set_sensitive(btnTambahKat,FALSE);
    gtk_widget_set_sensitive(btnEditKat,FALSE);
    gtk_widget_set_sensitive(btnHapusKat,FALSE);
    gtk_widget_grab_focus(entryNamaKat);
}

void
on_btnSimpanKat_clicked(GtkWidget *widget, gpointer user_data)
{
    GtkTextBuffer *buffer;
    gchar *text;
    GtkTextIter  start, end;
    const gchar *id = gtk_entry_get_text(GTK_ENTRY(entryIDKat));
    const gchar *kat = gtk_entry_get_text(GTK_ENTRY(entryNamaKat));

    //buffer
    gtk_widget_set_sensitive (katTextview,FALSE);
    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (katTextview));
    gtk_text_buffer_get_start_iter (buffer, &start);
    gtk_text_buffer_get_end_iter (buffer, &end);
    text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
    gtk_text_buffer_set_modified (buffer, FALSE);
    gtk_widget_set_sensitive (katTextview, TRUE);
    //masih satu lagi menggunakan buffer

    char query[2000];
    if(tmbh==1)
    {

        sprintf(query,"insert into kategori(nama_kat,ket) values('%s','%s')",kat,text);
        printf("%s",query);
        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
        if(mysql_query(conn, query)!=0)
        {
            /*ini mestinya pakai dialog*/
            print_error(conn, "INSERT statement failed");
        }
        else
        {
            /*ini juga harus pakai dialog*/
            printf("INSERT statement succeeded: %lu rows affected\n",
                   (unsigned long) mysql_affected_rows(conn));
        }
        g_free(text);
    }
    else if(tmbh==2)
    {

        sprintf(query,"update jenis set nama_kat='%s' ket='%s' where id_kat=%s ",kat,text,id);

        g_print("%s",query);
        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
        if(mysql_query(conn, query)!=0)
        {
            /*ini mestinya pakai dialog*/
            print_error(conn, "INSERT statement failed");
        }
        else
        {
            /*ini juga harus pakai dialog*/
            printf("INSERT statement succeeded: %lu rows affected\n",
                   (unsigned long) mysql_affected_rows(conn));
        }
        g_free(text);
    }
    on_btnBatalKat_clicked(widget, user_data);
    //mysql_close(conn);
}

void
on_btnBatalKat_clicked(GtkWidget *widget, gpointer user_data)
{
    GtkTextBuffer *buffer;
    //tmbh=1;
    gtk_entry_set_text(GTK_ENTRY(entryIDKat),"");
    gtk_entry_set_text(GTK_ENTRY(entryNamaKat),"");
    //buffer
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(katTextview),GTK_WRAP_WORD);
    gtk_widget_set_sensitive(katTextview,FALSE);
    buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(katTextview));
    gtk_text_buffer_set_text(buffer,"",-1);
    gtk_text_buffer_set_modified(buffer,FALSE);
    gtk_widget_set_sensitive(katTextview,TRUE);

    gtk_widget_set_sensitive(entryIDKat,FALSE);
    gtk_widget_set_sensitive(entryNamaKat,FALSE);
    gtk_widget_set_sensitive(btnSimpanKat,FALSE);
    gtk_widget_set_sensitive(btnBatalKat,FALSE);
    gtk_widget_set_sensitive(btnTambahKat,TRUE);
    gtk_widget_set_sensitive(btnEditKat,TRUE);
    gtk_widget_set_sensitive(btnHapusKat,TRUE);
}

void
on_btnEditKat_clicked(GtkWidget *widget, gpointer user_data)
{
    tmbh=2;

    GtkListStore *store;
    GtkTreeModel *model;
    GtkTextBuffer *buffer;
    GtkTreeIter  iter;
    gchar *ID,*KATEGORI,*KETERANGAN; //menggunakan pointer

    //coba2
    selection=gtk_tree_view_get_selection(GTK_TREE_VIEW(katView));
    //jenisView=setup_jenis_view();
    store = GTK_LIST_STORE(gtk_tree_view_get_model(
                               GTK_TREE_VIEW (katView))); //create_list();

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (katView));

    if (gtk_tree_model_get_iter_first(model, &iter) == FALSE)
        return;

    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection),
                                        &model, &iter))
    {
        //gtk_list_store_remove(store, &iter);
        gtk_tree_model_get(model, &iter,0,&ID,1,&KATEGORI,2,&KETERANGAN,-1);

        g_print("%s is selected \n %s\n %s",ID,KATEGORI,KETERANGAN);
        gtk_entry_set_text(GTK_ENTRY(entryIDKat),ID);
        gtk_entry_set_text(GTK_ENTRY(entryNamaKat),KATEGORI);
        //masih satu lagi
        //buffer
        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(katTextview),GTK_WRAP_WORD);
        gtk_widget_set_sensitive(katTextview,FALSE);
        buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(katTextview));
        gtk_text_buffer_set_text(buffer,KETERANGAN,-1);
        gtk_text_buffer_set_modified(buffer,FALSE);
        gtk_widget_set_sensitive(katTextview,TRUE);

        gtk_widget_set_sensitive(entryIDKat,FALSE);
        gtk_widget_set_sensitive(entryNamaKat,TRUE);
        gtk_widget_set_sensitive(btnSimpanKat,TRUE);
        gtk_widget_set_sensitive(btnBatalKat,TRUE);
        gtk_widget_set_sensitive(btnTambahKat,FALSE);
        gtk_widget_set_sensitive(btnEditKat,FALSE);
        gtk_widget_set_sensitive(btnHapusKat,FALSE);
        gtk_widget_grab_focus(entryNamaKat);

        g_free(ID);
        g_free(KATEGORI);
        g_free(KETERANGAN);
    }
}

void
on_btnHapusKat_clicked(GtkWidget *widget, gpointer selection)
{
    GtkListStore *store;
    GtkTreeModel *model;

    GtkTreeIter  iter;
    gchar *ID; //menggunakan pointer
    //coba2
    selection=gtk_tree_view_get_selection(GTK_TREE_VIEW(katView));
    //jenisView=setup_jenis_view();
    store = GTK_LIST_STORE(gtk_tree_view_get_model(
                               GTK_TREE_VIEW (katView))); //create_list();

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (katView));

    if (gtk_tree_model_get_iter_first(model, &iter) == FALSE)
        return;

    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection),
                                        &model, &iter))
    {
        //gtk_list_store_remove(store, &iter);
        gchar query[2000];
        gtk_tree_model_get(model, &iter,ID_KAT,&ID,-1);

        sprintf(query,"delete from kategori where id_kat='%s'",ID);

        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);
        if(mysql_query(conn, query)!=0)
        {
            /*ini mestinya pakai dialog*/
            print_error(conn, "INSERT statement failed");
        }
        else
        {
            /*ini juga harus pakai dialog*/
            printf("INSERT statement succeeded: %lu rows affected\n",
                   (unsigned long) mysql_affected_rows(conn));
        }
        g_free(ID);
        //mysql_close(conn);
    }
}

void
on_btnCloseKat_clicked(GtkWidget* widget, gpointer user_data)
{
    gtk_widget_hide(GTK_WIDGET(kategori));
}

//combo jenis
void
setup_combo_jenis(GtkWidget *cbJenis)
{
    GtkCellRenderer *renderer;
    GtkTreeModel *jmodel;
    //GtkCellLayout *layout;
    jmodel=createJenislist();
    gtk_combo_box_set_model(GTK_COMBO_BOX(cbJenis),jmodel);

    renderer = gtk_cell_renderer_text_new();
    /* Pack it into the combo box. */
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( cbJenis ), renderer, TRUE );
    /* Connect renderer to data source. */
    gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( cbJenis ), renderer, "text", 0,NULL );

    renderer = gtk_cell_renderer_text_new();
    /* Pack it into the combo box. */
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( cbJenis ), renderer, TRUE );
    /* Connect renderer to data source. */
    gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( cbJenis ), renderer, "text", 1,NULL );

}

//combo Kategori
void
setup_combo_kategori(GtkWidget *cbKategori)
{
    GtkCellRenderer *renderer;
    GtkTreeModel *kmodel;
    //GtkCellLayout *layout;
    kmodel=createKatlist();
    gtk_combo_box_set_model(GTK_COMBO_BOX(cbKategori),kmodel);

    renderer = gtk_cell_renderer_text_new();

    /* Pack it into the combo box. */
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( cbKategori ), renderer, TRUE );

    /* Connect renderer to data source. */
    gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( cbKategori ), renderer, "text", 0, NULL );

    renderer = gtk_cell_renderer_text_new();

    /* Pack it into the combo box. */
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( cbKategori ), renderer, TRUE );

    /* Connect renderer to data source. */
    gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( cbKategori ), renderer, "text", 1, NULL );
}

//combo Penerbit
void
setup_combo_penerbit(GtkWidget *cbPenerbit)
{
    GtkCellRenderer *renderer;
    GtkTreeModel *pmodel;
    //GtkCellLayout *layout;
    pmodel=createPenlist();
    gtk_combo_box_set_model(GTK_COMBO_BOX(cbPenerbit),pmodel);

    renderer = gtk_cell_renderer_text_new();

    /* Pack it into the combo box. */
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( cbPenerbit ), renderer, TRUE );

    /* Connect renderer to data source. */
    gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( cbPenerbit ), renderer, "text", 0, NULL );

    renderer = gtk_cell_renderer_text_new();

    /* Pack it into the combo box. */
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( cbPenerbit ), renderer, TRUE );

    /* Connect renderer to data source. */
    gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( cbPenerbit ), renderer, "text", 1, NULL );
}

//koleksi
//kol list
void
setup_kol_view (GtkWidget *kolView)
{
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;

    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("ID", renderer, "text", ID_KOL, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kolView), column);

    //render column 2
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Judul", renderer, "text", JUDUL, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kolView), column);

    //render column 3
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Jenis", renderer, "text", kolJENIS, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kolView), column);

    //render column 4
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Kategori", renderer, "text", kolKATEGORI, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kolView), column);

    //render column 5
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Penerbit", renderer, "text", kolPENERBIT, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kolView), column);

    //render column 6
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Tahun", renderer, "text", TAHUN, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kolView), column);

    //render column 7
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Deskripsi", renderer, "text", DESKRIPSI, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kolView), column);
}

void
createKollist(GtkWidget *kolView)
{
    GtkListStore *store;
    GtkTreeIter iter;
    gchar *message,*sql;

    store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(kolView)));
    gtk_list_store_clear(store);


    sql=g_strdup_printf("select dokumen.id_dok,dokumen.judul,dokumen.deskripsi,jenis.nama_jenis,\
                        kategori.nama_kat,penerbit.nama_pen,dokumen.tahun \
                        from dokumen,jenis,kategori,penerbit where dokumen.id_jenis=jenis.id_jenis\
                        and dokumen.id_kat=kategori.id_kat\
                        and dokumen.id_penerbit=penerbit.id_penerbit");
    //limit %i,%i", pageOffset,rowperpage)
    conn=mysql_init(NULL);
    mysql_real_connect(conn,host,user,pass,db,0,NULL,0);

    mysql_query(conn, sql);
    res_result=mysql_store_result(conn);
    num_fields=mysql_num_fields(res_result);
    num_rows=mysql_num_rows(res_result);

    while ((row=mysql_fetch_row(res_result)))
    {
        //menampilan row sebagai kolom yaitu pada row[0] dan row[1]
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            ID_KOL, row[0],
                            JUDUL,row[1],
                            DESKRIPSI, row[2],
                            kolJENIS,row[3],
                            kolKATEGORI, row[4],
                            kolPENERBIT,row[5],
                            TAHUN,row[6],
                            -1);
    }

    //status Table
    message=g_strdup_printf("Jumlah Koleksi saat ini adalah: %i buah",num_rows);
    gtk_statusbar_pop(GTK_STATUSBAR(statusTable),statustable_context_id);
    gtk_statusbar_push(GTK_STATUSBAR(statusTable),statustable_context_id,message);

// TODO (adeka#1#): Navigasi Paging toggleButton konteks    //nonaktifin dulu
    gtk_widget_set_sensitive(searchBtn,FALSE);
    gtk_widget_set_sensitive(searchEntry,FALSE);
    gtk_widget_set_sensitive(awal,FALSE);
    gtk_widget_set_sensitive(prev,FALSE);
    gtk_widget_set_sensitive(next,FALSE);
    gtk_widget_set_sensitive(end,FALSE);
    gtk_widget_set_sensitive(togJudul,FALSE);
    gtk_widget_set_sensitive(togDesk,FALSE);
    gtk_widget_set_sensitive(togPen,FALSE);
    gtk_widget_set_sensitive(togTeks,FALSE);

    //free memory
    mysql_free_result(res_result);
    g_free(sql);
    g_free(message);
    //mysql_close(conn);
}

void
setup_kol_model(GtkWidget *kolView)
{
    GtkListStore *store;

    store = gtk_list_store_new (kolCOLUMNS, G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,
                                G_TYPE_STRING,G_TYPE_STRING, G_TYPE_STRING,G_TYPE_STRING);
    gtk_tree_view_set_model (GTK_TREE_VIEW (kolView), GTK_TREE_MODEL (store));
    g_object_unref (store);

    createKollist (kolView);
}

void
on_btnOkKol_clicked(GtkWidget *widget, gpointer user_data)
{
    GtkTextBuffer *buffer;
    gchar *text;
    GtkTextIter  start, end;
    gchar *filename=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fileChooser));
    gchar *file_name=g_path_get_basename(filename);
    const gchar *id = gtk_entry_get_text(GTK_ENTRY(entryID));
    const gchar *jdl = gtk_entry_get_text(GTK_ENTRY(entryJdl));
    const gchar *thn=gtk_entry_get_text(GTK_ENTRY(tahun));
    //conn=mysql_init(NULL);
    //menghitung row utk mengambil id utk thumbnail
    mysql_query(conn,"select id_dok from dokumen");
    res_result=mysql_store_result(conn);
    num_rows=mysql_num_rows(res_result);
    //thumbnailing
    gchar thumb[2000];
    gchar buff[2000];
    sprintf(thumb,"%s[0]",filename);
    //menggunakan perintah convert dari imagemagick dengan ukuran lebar adalah 150px
    sprintf(buff,"convert -thumbnail 150x %s gbr/%i.png",thumb,num_rows+1);
    system(buff);
    mysql_free_result(res_result);

    //binary blob dan masih bingung untuk data lebih dari 1 Mb
    int len, size;
    char data[1000*1024];
    char chunk[2*1000*1024+1];
    char query[1024*5000];

    FILE *fp;
    fp = fopen(filename, "rb");
    size = fread(data, 1, 1024*1000, fp);

    gchar *idJenis=gtk_combo_box_get_active_text(GTK_COMBO_BOX(cbJenis));
    gchar *idKat=gtk_combo_box_get_active_text(GTK_COMBO_BOX(cbKategori));
    gchar *idPen=gtk_combo_box_get_active_text(GTK_COMBO_BOX(cbPenerbit));

    //buffer
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(desText),GTK_WRAP_WORD);
    gtk_widget_set_sensitive (desText,FALSE);
    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (desText));
    gtk_text_buffer_get_start_iter (buffer, &start);
    gtk_text_buffer_get_end_iter (buffer, &end);
    text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
    gtk_text_buffer_set_modified (buffer, FALSE);
    gtk_widget_set_sensitive (desText, TRUE);

    //gchar query[2000];
    mysql_real_escape_string(conn, chunk, data, size);

    if(tmbh==1)
    {
        //mysql_real_connect(conn,host,user,pass,db,0,NULL,0);

        char *stat = "INSERT INTO dokumen(judul,deskripsi,id_jenis,id_kat,id_penerbit,tahun,nama_file,file,timestamp)\
                     VALUES('%s','%s','%s','%s','%s','%s','%s', '%s',NOW())";
        //char *stat2=sprintf(stat,)
        len = snprintf(query, sizeof(stat)+sizeof(chunk) , stat,jdl,text,idJenis,idKat,idPen,thn,file_name,chunk);
        mysql_real_query(conn, query, len);
        printf("%i",len);

    }
    else if (tmbh==2)
    {
        char *stat = "update dokumen set judul='%s' deskripsi='%s' id_jenis='%s' id_kat='%s' id_penerbit='%s'\
                    tahun='%s' nama_file='%s' file='%s' timestamp=NOW() where id_dok='%s'";

        //char *stat2=sprintf(stat,)
        len = snprintf(query, sizeof(stat)+sizeof(chunk) , stat,jdl,text,idJenis,idKat,idPen,thn,file_name,chunk,id);
        mysql_real_query(conn, query, len);

    }

    g_free(idJenis);
    g_free(idKat);
    g_free(idPen);
//    g_free(thn);
//    g_free(jdl);
    g_free(text);
    g_free(filename);
    g_free(file_name);

    fclose(fp);
    //mysql_close(conn);
    on_btnBatalKol_clicked(widget,user_data);
    setup_kol_model(kolView);
    //update();
}

void
on_btnBatalKol_clicked(GtkWidget *widget,gpointer user_data)
{
    GtkTextBuffer *buffer;

    gtk_widget_set_sensitive(entryID,FALSE);
    gtk_entry_set_text(GTK_ENTRY(entryJdl),"");
    gtk_entry_set_text(GTK_ENTRY(tahun),"");
    gtk_combo_box_set_active(GTK_COMBO_BOX(cbJenis),-1);
    gtk_combo_box_set_active(GTK_COMBO_BOX(cbKategori),-1);
    gtk_combo_box_set_active(GTK_COMBO_BOX(cbPenerbit),-1);
    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fileChooser),"");

    //buffer
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(desText),GTK_WRAP_WORD);
    gtk_widget_set_sensitive(desText,FALSE);
    buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(desText));
    gtk_text_buffer_set_text(buffer,"",-1);
    gtk_text_buffer_set_modified(buffer,FALSE);
    gtk_widget_set_sensitive(desText,TRUE);
}

//select main row
gboolean
view_selection_func (GtkTreeSelection *selection,
                     GtkTreeModel     *model,
                     GtkTreePath      *path,
                     gboolean          path_currently_selected,
                     gpointer          userdata)
{
    GtkTreeIter iter;

    if (gtk_tree_model_get_iter(model, &iter, path))
    {
        gchar *name,*id;
        gchar file[200];

        gtk_tree_model_get(model, &iter, ID_KOL,&id,JUDUL, &name, -1);

        if (!path_currently_selected)
        {
            //g_print ("%s is going to be selected.\n", name);
            gtk_statusbar_pop(GTK_STATUSBAR(statusMain),statusbar_context_id);
            gtk_statusbar_push(GTK_STATUSBAR(statusMain),statusbar_context_id,name);
            //harusnya bisa menampilkan gambar cover
            sprintf(file,"gbr/%s.png",id);
            //printf(file);
            gtk_image_set_from_file(GTK_IMAGE(cover),file);
        }

        g_free(name);
        g_free(id);
    }

    return TRUE; /* allow selection state to change */
}

void
on_cari_clicked(GtkWidget *widget, gpointer user_data)
{
    GtkTreeModel       *model;
    GtkTreeIter        *iter;
    const gchar *text;

    if(col==1)
    {
        /*GValue value = {0, };
        gtk_tree_model_get_value(model, iter, JUDUL, &value);
        fprintf(stdout, "You have selected %s\n", g_value_get_string(&value));
        g_value_unset(&value);
        */
    }
    else if(col==2)
    {

    }
    else if(col==3)
    {

    }

}

//toggle
void
on_togglebutton1_toggled(GtkToggleButton *toggle, gpointer user_data)
{
    col=1;
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(togJudul)))
    {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togDesk),FALSE);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togPen),FALSE);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togTeks),FALSE);
    }
}

void
on_togglebutton2_toggled(GtkToggleButton *toggle, gpointer user_data)
{
    col=2;
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(togDesk)))
    {

        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togJudul),FALSE);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togPen),FALSE);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togTeks),FALSE);
    }
}

void
on_togglebutton3_toggled(GtkToggleButton *toggle, gpointer user_data)
{
    col=3;
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(togPen)))
    {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togJudul),FALSE);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togDesk),FALSE);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togTeks),FALSE);
    }
}

void
on_togglebutton4_toggled(GtkToggleButton *toggle, gpointer user_data)
{
    col=4;
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(togTeks)))
    {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togJudul),FALSE);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togPen),FALSE);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togDesk),FALSE);
    }
}

